package game;

import cards.*;
import java.io.PrintStream;
import java.util.*;

public class theGame
{

    public List currentHand;
    public List activeHand;
    public List playedCards;
    public int currentHandType;
    public int control;
    public boolean threeOfSpades;
    public hand p0;
    public hand p1;
    public hand p2;
    public hand p3;
    public static int score0 = 0;
    public static int score1 = 0;
    public static int score2 = 0;
    public static int score3 = 0;

    public theGame(hand p0, hand p1, hand p2, hand p3, int control)
    {
        this.control = control;
        this.p0 = p0;
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
        currentHand = null;
        activeHand = null;
        playedCards = null;
    }

    public List countingcards()
    {
        List countinghand = new ArrayList();
        countinghand.addAll(p0.theHand);
        countinghand.addAll(p1.theHand);
        countinghand.addAll(p2.theHand);
        countinghand.addAll(p3.theHand);
        return countinghand;
    }

    public card medianCardValue()
    {
        List l = countingcards();
        for(int i = 0; i < l.size(); i++)
        {
            for(int j = 0; j < l.size() - 1; j++)
            {
                card tempcard1 = (card)l.get(j);
                card tempcard2 = (card)l.get(j + 1);
                if(tempcard1.getValue() > tempcard2.getValue())
                {
                    l.remove(j);
                    l.remove(j);
                    l.add(j, tempcard1);
                    l.add(j, tempcard2);
                }
            }

        }

        return (card)l.get(l.size() / 2);
    }

    public boolean lowCardWarn()
    {
        return p0.cardsLeft() < 3 || p1.cardsLeft() < 3 || p2.cardsLeft() < 3 || p3.cardsLeft() < 3;
    }

    public boolean lowCardWarn2()
    {
        return p0.cardsLeft() < 2 || p1.cardsLeft() < 2 || p2.cardsLeft() < 2 || p3.cardsLeft() < 2;
    }

    public void printGameState()
    {
        System.out.println();
        System.out.println("******GAME STATE******");
        System.out.println("Cards Left: P0:" + p0.cardsLeft() + " ,P1:" + p1.cardsLeft() + " ,P2:" + p2.cardsLeft() + ", P3:" + p3.cardsLeft());
        System.out.println("Active Hand: " + activeHand);
        System.out.println("Player with Control: #" + control);
        System.out.println("**********************");
        System.out.println();
    }

    boolean hasHand(List curHand, int player)
    {
        if(curHand == null)
        {
            System.out.println("BUG?");
            return false;
        }
        Iterator iter;
        for(iter = curHand.iterator(); iter.hasNext();)
        {
            card c = (card)iter.next();
            boolean b;
            switch(player)
            {
            case 0: // '\0'
                b = p0.theHand.contains(c);
                break;

            case 1: // '\001'
                b = p1.theHand.contains(c);
                break;

            case 2: // '\002'
                b = p2.theHand.contains(c);
                break;

            case 3: // '\003'
                b = p3.theHand.contains(c);
                break;

            default:
                b = false;
                break;
            }
            if(!b)
            {
                return false;
            }
        }

        iter = curHand.iterator();
        List newList = new ArrayList();
        card c;
        for(; iter.hasNext(); newList.add(c))
        {
            c = (card)iter.next();
            if(newList.contains(c))
            {
                return false;
            }
        }

        return true;
    }

    public boolean acceptor(List curHand, int player)
    {
        currentHand = curHand;
        if(!hasHand(curHand, player))
        {
            return false;
        }
        if(control == player)
        {
            int z = handType();
            if(z == -1)
            {
                return false;
            }
            currentHandType = z;
            switch(player)
            {
            case 0: // '\0'
                p0.delHandFromHand(currentHand);
                break;

            case 1: // '\001'
                p1.delHandFromHand(currentHand);
                break;

            case 2: // '\002'
                p2.delHandFromHand(currentHand);
                break;

            case 3: // '\003'
                p3.delHandFromHand(currentHand);
                break;
            }
            activeHand = currentHand;
        } else
        if(validHand() && beatsHand())
        {
            currentHandType = handType();
            switch(player)
            {
            case 0: // '\0'
                p0.delHandFromHand(currentHand);
                break;

            case 1: // '\001'
                p1.delHandFromHand(currentHand);
                break;

            case 2: // '\002'
                p2.delHandFromHand(currentHand);
                break;

            case 3: // '\003'
                p3.delHandFromHand(currentHand);
                break;
            }
            activeHand = currentHand;
            control = player;
        } else
        {
            return false;
        }
        return true;
    }

    private boolean beatsHand()
    {
        if(currentHandType == 1 && handType() == 4)
        {
            return ((card)activeHand.get(0)).getValue() == 12;
        }
        int size = activeHand.size() - 1;
        card comp0 = (card)activeHand.get(size);
        card comp1 = (card)currentHand.get(size);
        if(comp0.getValue() == comp1.getValue())
        {
            return comp0.getSuitValue() < comp1.getSuitValue();
        }
        return comp0.getValue() < comp1.getValue();
    }

    boolean validHand()
    {
        return currentHandType == handType() && currentHandType != -1 || currentHandType == 1 && handType() == 4;
    }

    public int handType()
    {
        currentHand.add(null);
        if(currentHand.get(1) == null)
        {
            currentHand.remove(null);
            return 1;
        }
        if(((card)currentHand.get(1)).getValue() == ((card)currentHand.get(0)).getValue())
        {
            if(currentHand.get(2) == null)
            {
                currentHand.remove(null);
                return 2;
            }
            if(((card)currentHand.get(2)).getValue() == ((card)currentHand.get(1)).getValue() + 1)
            {
                if(currentHand.get(3) == null || ((card)currentHand.get(3)).getValue() != ((card)currentHand.get(2)).getValue())
                {
                    return -1;
                }
                if(currentHand.get(4) == null || ((card)currentHand.get(4)).getValue() != ((card)currentHand.get(2)).getValue() + 1)
                {
                    return -1;
                }
                if(currentHand.get(5) == null || ((card)currentHand.get(5)).getValue() != ((card)currentHand.get(4)).getValue())
                {
                    return -1;
                }
                if(currentHand.get(6) == null)
                {
                    currentHand.remove(null);
                    return 20;
                }
            }
            if(((card)currentHand.get(1)).getValue() == ((card)currentHand.get(2)).getValue() && currentHand.get(3) == null)
            {
                currentHand.remove(null);
                return 3;
            }
            if(((card)currentHand.get(2)).getValue() == ((card)currentHand.get(3)).getValue())
            {
                if(currentHand.get(4) == null)
                {
                    currentHand.remove(null);
                    return 4;
                } else
                {
                    currentHand.remove(null);
                    return -1;
                }
            } else
            {
                currentHand.remove(null);
                return -1;
            }
        }
        currentHand.remove(null);
        if(currentHand.size() < 3)
        {
            return -1;
        } else
        {
            return straightCount(0);
        }
    }

    private int straightCount(int c)
    {
        if(((card)currentHand.get(currentHand.size() - 1)).getValue() == 12)
        {
            return -1;
        }
        currentHand.add(null);
        if(c == 0)
        {
            if(((card)currentHand.get(0)).getValue() == ((card)currentHand.get(1)).getValue() - 1 && ((card)currentHand.get(1)).getValue() == ((card)currentHand.get(2)).getValue() - 1)
            {
                if(currentHand.get(3) == null)
                {
                    currentHand.remove(null);
                    return 5;
                } else
                {
                    currentHand.remove(null);
                    return straightCount(3);
                }
            }
        } else
        if(((card)currentHand.get(c - 1)).getValue() == ((card)currentHand.get(c)).getValue() - 1)
        {
            if(currentHand.get(c + 1) == null)
            {
                currentHand.remove(null);
                return c + 3;
            } else
            {
                currentHand.remove(null);
                return straightCount(c + 1);
            }
        }
        currentHand.remove(null);
        return -1;
    }

    public List inputToHand(String s)
    {
        int absval = 0;
        List l = new ArrayList();
        for(int i = 0; i < s.length(); i++)
        {
            if(s.charAt(i) == 's' || s.charAt(i) == 'S')
            {
                absval = 0;
            } else
            if(s.charAt(i) == 'c' || s.charAt(i) == 'C')
            {
                absval = 13;
            } else
            if(s.charAt(i) == 'd' || s.charAt(i) == 'D')
            {
                absval = 26;
            } else
            if(s.charAt(i) == 'h' || s.charAt(i) == 'H')
            {
                absval = 39;
            }
            if(s.charAt(i) == '0' || s.charAt(i) == '1' || s.charAt(i) == '2' || s.charAt(i) == '3' || s.charAt(i) == '4' || s.charAt(i) == '5' || s.charAt(i) == '6' || s.charAt(i) == '7' || s.charAt(i) == '8' || s.charAt(i) == '9')
            {
                if(i + 1 < s.length() && (s.charAt(i + 1) == '0' || s.charAt(i + 1) == '1' || s.charAt(i + 1) == '2' || s.charAt(i + 1) == '3' || s.charAt(i + 1) == '4' || s.charAt(i + 1) == '5' || s.charAt(i + 1) == '6' || s.charAt(i + 1) == '7' || s.charAt(i + 1) == '8' || s.charAt(i + 1) == '9'))
                {
                    absval += 10;
                } else
                {
                    int x = s.charAt(i) - 48;
                    absval += x;
                }
            }
            if(s.charAt(i) == ',' || i + 1 >= s.length())
            {
                l.add(deck.unshuffledDeck.get(absval));
            }
        }

        return l;
    }

}
